package com.promise.jokerdream.ui.webview

import android.os.Message
import android.util.Log
import android.webkit.WebChromeClient
import android.webkit.WebView

/**
 * 自定义WebChromeClient
 * 处理进度更新、新窗口创建等功能
 */
class CustomWebChromeClient(
    private val callback: IWebViewCallback
) : WebChromeClient() {

    private val TAG = "CustomWebChromeClient"

    /**
     * 处理新窗口创建
     * 恢复对新窗口的处理，保证 target=_blank 流程正常
     */
    override fun onCreateWindow(view: WebView?, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message?): Boolean {
        val result = resultMsg ?: return false
        val transport = result.obj as? WebView.WebViewTransport ?: return false
        transport.webView = view
        result.sendToTarget()
        return true
    }
    
    /**
     * 处理进度变化
     */
    override fun onProgressChanged(view: WebView?, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        callback.onProgressChanged(newProgress)
    }
}
